/*
 * PayloadMessage.h
 *
 *  Created on: 25.06.2011
 *      Author: mario
 */

#ifndef PAYLOADMESSAGE_H_
#define PAYLOADMESSAGE_H_

#include "ariba/ariba.h"
#include "../../Payload.h"

using_serialization;

class PayloadMessage : public ariba::Message
{
  VSERIALIZEABLE;
public:
  PayloadMessage();
  PayloadMessage(PayloadPtr payload);
  virtual ~PayloadMessage();

//  void set_payload(PayloadPtr payload);
  PayloadPtr get_payload();

private:
  PayloadPtr payload;
};


//sznBeginDefault(PayloadMessage, X)
//{
//  X && help_size;
//  X && ArrayTpl<uint8_t>(help_ptr, help_size);
//
//} sznEnd();


sznBeginDefault(PayloadMessage, X)
{
  string key;
  uint32_t size;
  uint64_t pos;

  // BRANCH: incoming
  if ( X.isDeserializer() )
  {
    X && size;
    X && pos;
    X && T(key);

    PayloadPtr x(new Payload_Data(size));
    payload = x;

//    uint8_t* ser_buffer = X.bytes(size);
////    printf("***** BYTES: '%p'\n", ser_buffer);
//    memcpy(payload->raw_pointer(), ser_buffer, size);
    X && StaticArrayTpl<uint8_t>(payload->raw_pointer(), size);


    payload->set_key(key);
    payload->set_position(pos);
  }
  // BRANCH: outgoing
  else
  {
    key = payload->get_key();
    size = payload->get_size();
    pos = payload->get_postion();

    X && size;
    X && pos;
    X && T(key);

    // payload
//    uint8_t* ser_buffer = X.bytes(size);
////    printf("***** BYTES: '%p'\n", ser_buffer);
//    if ( ser_buffer != 0 )
//        memcpy(ser_buffer, payload->raw_pointer(), size);
    X && StaticArrayTpl<uint8_t>(payload->raw_pointer(), size);
  }

} sznEnd();

#endif /* PAYLOADMESSAGE_H_ */
